#ifndef CURRENCY_H
#define CURRENCY_H

#include <iostream>
#include <string>
#include <exception>
// 异常类
class illegalParameterValue : public std::exception
{
private:
    std::string message;
public:
    illegalParameterValue() :message("Illegal parameter value") {}
    illegalParameterValue(std::string theMessage) { message = theMessage; }
    virtual const char* what()
    {
        return message.c_str();
    }
};

// 符号类型
enum signType { plus, minus };
// 货币类
class Currency
{
public:
    // 构造函数
    Currency(signType theSign = plus, unsigned long theDollars = 0, unsigned int theCents = 0);
    // 析构函数
    ~Currency() {}
    // 设置值
    void setValue(signType, unsigned long, unsigned int);
    // 设置值
    void setValue(double);
    // 获取符号
    signType getSign() const { return sign; }
    // 获取美元
    unsigned long getDollars() const { return dollars; }
    // 获取美分
    unsigned int getCents() const { return cents; }
    Currency add(const Currency&) const;
    Currency& increment(const Currency&);
    void output() const;
private:
    signType sign; // 符号
    unsigned long dollars; // 美元
    unsigned int cents; // 美分
};

// 新的Currency类
class Currency2
{
private:
    long amount;
public:
    Currency2(signType theSign = plus, unsigned long theDollars = 0, unsigned int theCents = 0);
    Currency2(double theAmount);
    ~Currency2() {}
    void setValue(signType, unsigned long, unsigned int);
    void setValue(double);
    signType getSign() const { return (amount < 0) ? minus : plus; }
    unsigned long getDollars() const { return (amount < 0) ? (-amount) / 100 : amount / 100; }
    unsigned int getCents() const { return (amount < 0) ? -amount % 100 : amount % 100; }
    Currency2 add(const Currency2&) const;
    Currency2& increment(const Currency2& x) { amount += x.amount; return *this; };
    void output() const;
};

#endif // CURRENCY_H
